package com.resource.noticemanage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.resource.common.utils.PageUtils;
import com.resource.common.utils.Query;
import com.resource.common.utils.R;
import com.resource.homeworkmanage.entity.TMessageNoticeEntity;
import com.resource.noticemanage.mapper.TMessageNoticeDao;
import com.resource.noticemanage.service.TMessageNoticeService;
import com.resource.studentmanage.feign.StudentDealRemote;
import com.resource.studentmanage.feign.StudentExamRemote;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;


@Service
public class TMessageNoticeServiceImpl extends ServiceImpl<TMessageNoticeDao, TMessageNoticeEntity> implements TMessageNoticeService {

    @Autowired
    private StudentExamRemote studentExamRemote;

    @Autowired
    private StudentDealRemote studentDealRemote;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int size = params.size();
        IPage<TMessageNoticeEntity> page=new Page<>();
        if(size==4){
            String keys="";
            String param="";
            Set<String> strings = params.keySet();
            for (String key : strings) {
                keys=key;
                param=params.get(keys).toString();
            }
            page = this.page(
                    new Query<TMessageNoticeEntity>().getPage(params),
                    new QueryWrapper<TMessageNoticeEntity>().eq(keys,param)
            );
            return new PageUtils(page);
        }
        page = this.page(
                new Query<TMessageNoticeEntity>().getPage(params),
                new QueryWrapper<TMessageNoticeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean submitNoticeByHomework(Map<String, String> map) {
        TMessageNoticeEntity tMessageNoticeEntity = new TMessageNoticeEntity();
        tMessageNoticeEntity.setPublishNo(map.get("publishNo"));
        tMessageNoticeEntity.setPublishName(map.get("publishName"));
        tMessageNoticeEntity.setPublishContent(map.get("publishContent"));
        tMessageNoticeEntity.setReceiveNo(map.get("receiveNo"));
        tMessageNoticeEntity.setReceiveName(map.get("receiveName"));
        tMessageNoticeEntity.setMessageType("作业");
        tMessageNoticeEntity.setPublishTime(map.get("publishTime"));
        tMessageNoticeEntity.setCheckResult("未处理");
        this.saveOrUpdate(tMessageNoticeEntity);
        return false;
    }

    @Override
    @Transactional
    public R dealMessageBatch(List<TMessageNoticeEntity> tMessageNotice) {
        if(tMessageNotice!=null && tMessageNotice.size()!=0) {
            //遍历
            for (TMessageNoticeEntity tMessageNoticeEntity : tMessageNotice) {
                //判断消息是否已经处理
                String checkResult = tMessageNoticeEntity.getCheckResult();
                if(!"已处理".equals(checkResult)){
                    return R.error("消息未经过处理，不允许提交反馈");
                }
                String messageType = tMessageNoticeEntity.getMessageType();
                if (StringUtils.isEmpty(messageType) || (!"作业".equals(messageType) && !"考试".equals(messageType))) {
                    return R.error("消息类型不是作业，考试，不支持提交反馈");
                }
                HashMap<String, String> stringMap = new HashMap<>();
                stringMap.put("studentCode",tMessageNoticeEntity.getPublishNo());

                stringMap.put("messageContent",tMessageNoticeEntity.getMessageContent());
                stringMap.put("teacherName",tMessageNoticeEntity.getReceiveName());
                if("作业".equals(messageType)){
                    studentDealRemote.submitStudentWork(stringMap);

                }else if("考试".equals(messageType)){
                    //考试成绩
                    stringMap.put("examGrade",tMessageNoticeEntity.getExamGrade());
                    studentExamRemote.submitMessageByExam(stringMap);
                }
            }
            return R.ok();
        }else{
            return R.error("未选中提交反馈消息");
        }
    }

    @Override
    public boolean submitNoticeByExam(Map<String, Object> map) {
        TMessageNoticeEntity tMessageNoticeEntity = new TMessageNoticeEntity();
        tMessageNoticeEntity.setMessageType("考试");
        //组装
        Object examStudentCode = map.get("examStudentCode");
        Object examStudentName = map.get("examStudentName");
        Object examTeacherCode = map.get("examTeacherCode");
        Object examTeacherName = map.get("examTeacherName");
        Object submitTime = map.get("submitTime");
        //分
        Object examContent = map.get("examContent");
        tMessageNoticeEntity.setCheckResult("未处理");
        tMessageNoticeEntity.setPublishContent(Objects.isNull(examContent) ? "":examContent.toString());
        tMessageNoticeEntity.setPublishNo(Objects.isNull(examStudentCode) ? "":examStudentCode.toString());
        tMessageNoticeEntity.setPublishName(Objects.isNull(examStudentName) ? "":examStudentName.toString());
        tMessageNoticeEntity.setReceiveName(Objects.isNull(examTeacherName) ? "":examTeacherName.toString());
        tMessageNoticeEntity.setPublishTime(Objects.isNull(submitTime) ? "":submitTime.toString());
        tMessageNoticeEntity.setReceiveNo(Objects.isNull(examTeacherCode) ? "":examTeacherCode.toString());
        this.saveOrUpdate(tMessageNoticeEntity);
        return true;
    }
}